package cn.iocoder.yudao.module.service.controller.admin.sceneconfig;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.service.controller.admin.sceneconfig.vo.*;
import cn.iocoder.yudao.module.service.dal.dataobject.sceneconfig.SceneConfigDO;
import cn.iocoder.yudao.module.service.service.sceneconfig.SceneConfigService;

@Tag(name = "管理后台 - 场景配置")
@RestController
@RequestMapping("/service/scene-config")
@Validated
public class SceneConfigController {

    @Resource
    private SceneConfigService sceneConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建场景配置")
    @PreAuthorize("@ss.hasPermission('service:scene-config:create')")
    public CommonResult<Long> createSceneConfig(@Valid @RequestBody SceneConfigSaveReqVO createReqVO) {
        return success(sceneConfigService.createSceneConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新场景配置")
    @PreAuthorize("@ss.hasPermission('service:scene-config:update')")
    public CommonResult<Boolean> updateSceneConfig(@Valid @RequestBody SceneConfigSaveReqVO updateReqVO) {
        sceneConfigService.updateSceneConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除场景配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('service:scene-config:delete')")
    public CommonResult<Boolean> deleteSceneConfig(@RequestParam("id") Long id) {
        sceneConfigService.deleteSceneConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得场景配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('service:scene-config:query')")
    public CommonResult<SceneConfigRespVO> getSceneConfig(@RequestParam("id") Long id) {
        SceneConfigDO sceneConfig = sceneConfigService.getSceneConfig(id);
        return success(BeanUtils.toBean(sceneConfig, SceneConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得场景配置分页")
    @PreAuthorize("@ss.hasPermission('service:scene-config:query')")
    public CommonResult<PageResult<SceneConfigRespVO>> getSceneConfigPage(@Valid SceneConfigPageReqVO pageReqVO) {
        PageResult<SceneConfigDO> pageResult = sceneConfigService.getSceneConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SceneConfigRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得场景配置列表")
    @PreAuthorize("@ss.hasPermission('service:scene-config:query')")
    public CommonResult<List<SceneConfigRespVO>> getSceneConfigList(@RequestParam("type") Integer type) {
        SceneConfigPageReqVO pageReqVO = new SceneConfigPageReqVO();
        pageReqVO.setType(type);
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SceneConfigDO> pageResult = sceneConfigService.getSceneConfigPage(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, SceneConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出场景配置 Excel")
    @PreAuthorize("@ss.hasPermission('service:scene-config:export')")
    @OperateLog(type = EXPORT)
    public void exportSceneConfigExcel(@Valid SceneConfigPageReqVO pageReqVO,
                                       HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SceneConfigDO> list = sceneConfigService.getSceneConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "场景配置.xls", "数据", SceneConfigRespVO.class,
                BeanUtils.toBean(list, SceneConfigRespVO.class));
    }

}